﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using GlobalHelper;

namespace Standardization
{
    public class CompanyFirst : IStandardIzation
    {
        /// <summary>
        /// 正规化时刻表
        /// </summary>
        /// <param name="origianlTimeTable"></param>
        /// <returns></returns>
        public DataTable StandardTimeTable(DataTable origianlTimeTable)
        {
            DataTable result = new DataTable();

            int rowAmount = origianlTimeTable.Rows.Count;
            int colAmount = origianlTimeTable.Columns.Count;

            IList<IList<string>> tableInfo = new List<IList<string>>();
            for (int i = 0; i < origianlTimeTable.Rows.Count; i++)
            {
                IList<string> line = Helper.GetLine(origianlTimeTable.Rows[i]);

                if (!IsEmptyLine(line))
                {
                    line = StandardLine(line);
                    tableInfo.Add(line);
                }
            }

            result = Helper.CreateDataTable(tableInfo, rowAmount, colAmount);

            return result;
        }





        /// <summary>
        /// 正规化数据行
        /// </summary>
        /// <param name="originalInfo"></param>
        /// <returns></returns>
        public static IList<string> StandardLine(IList<string> originalInfo)
        {
            if (IsEmptyLine(originalInfo))
            {
                return null;
            }

            IList<string> result = originalInfo;

            //1. 路牌名补标识(必须在补路牌名前)
            result = CompleteTokenFlag(result);

            //2. 补路牌名(必须在补路牌标识后)
            result = FillTokenName(result);

            //3. 规范化路牌结束
            result = FixTokenEnd(result);

            //4. 发车时间取整
            result = FixDeparture(result);

            //5. 合并下半班路牌与发车时间
            result = LocalHelper.CombinNightTokenName(result);

            return result;
        }

        /// <summary>
        /// 发车时间取整
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        private static IList<string> FixDeparture(IList<string> list)
        {
            IList<string> result = list;

            for (int i = 0; i < list.Count; i++)
            {
                if (Helper.CheckIsDecimal(list[i]))
                {
                    Double d = Convert.ToDouble(list[i]);
                    list[i] = ToBig(d);
                }
            }

            return result;
        }


        private static string ToBig(Double d)
        {
            string result = string.Empty;

            d += 0.5;
            result = Convert.ToInt32(d).ToString();

            return result;
        }

        /// <summary>
        ///  修正路牌结束标志
        ///  包括上半班与下半班
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        private static IList<string> FixTokenEnd(IList<string> list)
        {
            IList<string> result = list;

            try
            {
                for (int i = 0; i < result.Count; i++)
                {
                    if (LocalHelper.IsEndCompanyOne(result[i]))
                    {
                        string s = result[i];
                        s = Helper.RemoveContentFlag(s);
                        s += Helper.StandTokenEndFlag;
                        result[i] = s;
                    }
                }

            }
            catch (Exception ex)
            {

                throw;
            }

            return result;
        }

        /// <summary>
        /// 补路牌标识
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        private static IList<string> CompleteTokenFlag(IList<string> list)
        {
            IList<string> result = new List<string>();

            string tokenName = list[0].Trim();

            if (!tokenName.StartsWith(Helper.TokenBeginFlag))
            {
                tokenName = Helper.TokenBeginFlag + tokenName;
            }

            if (!tokenName.EndsWith(Helper.TokenEndFlag))
            {
                tokenName += Helper.TokenEndFlag;
            }

            result = list;

            result[0] = tokenName;

            return result;
        }

        /// <summary>
        /// 补路牌名
        /// </summary>
        /// <param name="line"></param>
        /// <returns></returns>
        private static IList<string> FillTokenName(IList<string> line)
        {
            IList<string> result = line;

            //1.1 取首列为路牌名
            string tokenName = result[0].ToString();

            //1.2 寻找第一个结束
            int firstEnd = 0;
            for (int i = 1; i < result.Count; i++)
            {
                if (LocalHelper.IsEndCompanyOne(result[i]))
                {
                    firstEnd = i;
                    break;
                }
            }

            //1.3 寻找结束后的第一个时间
            int firstTimeAfterFirstEnd = firstEnd + 1;
            for (int i = firstTimeAfterFirstEnd; i < result.Count; i++)
            {
                if (!string.IsNullOrEmpty(result[i]))
                {
                    firstTimeAfterFirstEnd = i;

                    //1.4 时间前加路牌名
                    result[firstTimeAfterFirstEnd - 1] = tokenName;

                    break;
                }
            }

            
            return result;
        }

        /// <summary>
        /// 判断是否为空行
        /// 1. 只有路牌起始,后面没有数据
        /// 2. 正行都是空的
        /// </summary>
        /// <param name="originalInfo"></param>
        /// <returns></returns>
        private static bool IsEmptyLine(IList<string> originalInfo)
        {
            if (originalInfo.Count <= 0 || string.IsNullOrEmpty(originalInfo[0]))
            {
                return true;
            }

            bool result = true;

            for (int i = 1; i < originalInfo.Count; i++)
            {
                if (!string.IsNullOrEmpty(originalInfo[i]))
                {
                    result = false;
                    break;
                }
            }

            return result;
        }
    }
}
